package cmd

import (
	"fmt"
	"log"
	"strings"
	"tour/internal/redis"

	"github.com/spf13/cobra"
)

const (
	GroupByC = iota + 1
	ListByC
	KillNormal
	DelScan
	GroupByClientCluster
	GroupByClientSentinel
	KillNormalCluster
	KeyScan
	KeyScanSentinel
	DelScanCluster
)

var redismode int8
var IP string
var Port string
var delPattern string
var timeWait int
var Sentinel string
var keyPattern string

// var RedisPassword string

var redisdesc = strings.Join([]string{
	"该子命令支持Redis，模式如下：",
	"1: 根据Client 的 IP汇总",
	"2: 根据Client 的 IP列举",
	"3: kill Client ,排除掉normal外的连接",
	"4: scan删除数据",
	"5: 根据Client 的 IP汇总 Cluster模式",
	"6:  根据Client 的 IP汇总, Sentinel模式",
	"7:  kill Client ,排除掉normal外的连接,, Cluster模式",
	"8:  scan数据，普通",
	"9:  scan数据，sentinel",
	"10:  scan删除数据，cluster",
}, "\n")

// todo 需要支持密码
var redisCmd = &cobra.Command{
	Use:   "redis",
	Short: "redis",
	Long:  redisdesc,
	Run: func(cmd *cobra.Command, args []string) {
		attr := IP + ":" + Port
		var content string
		switch redismode {
		case GroupByC:
			content = redis.GroupByClient(attr)
		case ListByC:
			content = redis.ListByClient(attr)
		case KillNormal:
			content = redis.KillNormal(attr)
		case DelScan:
			content = redis.DelScan(attr, delPattern, timeWait)
		case GroupByClientCluster:
			content = redis.GroupByClientCluster(attr)
		case GroupByClientSentinel:
			content = redis.GroupByClientSentinel(attr, Sentinel)
		case KillNormalCluster:
			content = redis.KillNormalCluster(attr)
		case KeyScan:
			content = redis.ScanDataRedis(attr, keyPattern, timeWait)
		case KeyScanSentinel:
			content = redis.ScanDataSentinel(attr, Sentinel, keyPattern, timeWait)
		case DelScanCluster:
			content = redis.DelScanCluster(attr, keyPattern, timeWait)
		default:
			log.Fatalf("暂不支持该转换模式，请执行 help redis 查看帮助文档")
		}

		//log.Println("输出结果")
		//fmt.Println()
		fmt.Println(content)
	},
}

func init() {
	redisCmd.Flags().StringVarP(&IP, "IP", "i", "127.0.0.1", "请输入连接的Redis IP")
	redisCmd.Flags().StringVarP(&Port, "Port", "p", "6379", "请输入连接的Redis Port")
	redisCmd.Flags().StringVarP(&Sentinel, "Sentinel", "s", "", "请输入连接的SentinelName")
	redisCmd.Flags().StringVarP(&delPattern, "delPattern", "d", "", "请输入需要scan删除的表达式")
	redisCmd.Flags().IntVarP(&timeWait, "timeWait", "t", 0, "scan每批次的等待时间")
	redisCmd.Flags().StringVarP(&keyPattern, "keyPattern", "k", "", "请输入需要scan查询的表达式")
	redisCmd.Flags().StringVarP(&redis.RedisPassword, "Password", "P", "", "请输入密码")
	redisCmd.Flags().Int8VarP(&redismode, "RedisMode", "m", 0, "请输入Redis工具的模式")
}
